package com.sudo.api.config;

import com.sudo.api.interceptor.AuthorizationInterceptor;
import com.sudo.api.interceptor.UserTokenInterceptor;
import com.sudo.common.component.AuthTokenProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 为道日损 2020-04-24 17:40
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private UserTokenInterceptor userTokenInterceptor;
    @Resource
    private AuthorizationInterceptor authorizationInterceptor;
    @Resource
    private AuthTokenProperties authTokenProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userTokenInterceptor).addPathPatterns("/**")
                .excludePathPatterns(authTokenPathList());
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**")
                .excludePathPatterns(authTokenPathList());
    }

    private List<String> authTokenPathList(){
        List<String> list = new ArrayList<>();
        // 系统接口
        list.add("/sysUser/userLogin");
        list.add("/sysUser/logout");
        list.add("/sysUser/isUserTokenRight");
        list.add("/sysUser/generateImageCode");
        list.add("/sysUser/userRegister");
        list.add("/sysUser/postEmailCode");

        list.addAll(authTokenProperties.getExcludePaths());
        return list;
    }

}